package com.doge.service.wx.search;

import com.doge.bean.pojo.keyword.Keyword;
import com.doge.bean.pojo.keyword.KeywordExample;
import com.doge.bean.pojo.searchhistory.SearchHistory;
import com.doge.bean.pojo.searchhistory.SearchHistoryExample;
import com.doge.bean.pojo.user.User;
import com.doge.bean.vo.search.WxSearchHelpVo;
import com.doge.bean.vo.search.WxSearchIndexVo;
import com.doge.mapper.GoodsMapper;
import com.doge.mapper.KeywordMapper;
import com.doge.mapper.SearchHistoryMapper;
import com.doge.util.auth.GetSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: doge
 * @description: 搜索的服务
 * @author: Mr.Jiang
 * @create: 2021-12-02 14:44
 **/
@Service
public class WxSearchServiceImpl implements WxSearchService{
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxSearchIndexVo index() {
        //查defaultKeyword集合取第一个
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.createCriteria().andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        Keyword defaultKeyword = keywords.get(0);
        //查hotKeyword集合
        KeywordExample keywordExample1 = new KeywordExample();
        keywordExample1.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<Keyword> hotKeywords = keywordMapper.selectByExample(keywordExample1);
        //查searchHistory
        User user = GetSubject.getUser();
        List<SearchHistory> searchHistories = null;
        if(user!=null){
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            searchHistoryExample.createCriteria().andUserIdEqualTo(GetSubject.getUserId()).andDeletedEqualTo(false);
            searchHistoryExample.setOrderByClause("update_time desc limit 5");
            searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setUserId(GetSubject.getUserId());
        }
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        wxSearchIndexVo.setDefaultKeyword(defaultKeyword);
        wxSearchIndexVo.setHotKeywordList(hotKeywords);
        wxSearchIndexVo.setHistoryKeywordList(searchHistories);
        return wxSearchIndexVo;
    }

    @Override
    public void clearHistory() {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        Integer id = GetSubject.getUserId();
        searchHistoryExample.createCriteria().andUserIdEqualTo(id);
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        searchHistoryMapper.updateByExampleSelective(searchHistory,searchHistoryExample);
    }

    @Override
    public WxSearchHelpVo helper(String keyword) {
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.createCriteria().andKeywordLike("%"+keyword+"%");
        List<Keyword> list = keywordMapper.selectByExample(keywordExample);
        ArrayList<String> arrayList = new ArrayList<>();
        for (Keyword keyword1 : list) {
            arrayList.add(keyword1.getKeyword());
        }
        WxSearchHelpVo wxSearchHelpVo = new WxSearchHelpVo();
        wxSearchHelpVo.setList(arrayList);
        return wxSearchHelpVo;
    }


}
